﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL;

namespace WebSite.admin.product
{
    public partial class openCard : BaseAdmin
    {
        readonly dalCompany theCompany = new dalCompany();
  
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //// 绑定 XML 数据源到树控件
                //treeMenu.DataSource = XmlDataSource1;
                //treeMenu.DataBind();

                //ResolveTreeNode(treeMenu.Nodes);
                if (base.CompanyLevelId == "4")
                {
                    Response.Redirect("openCardList.aspx?id=" + base.CompanyId);
                }
                else
                {
                    this.BindProvenctList();
                    this.BindCompanyLevel();
                    this.BindCompanyTree();
                }
            }
        }
        void BindCompanyLevel()
        {
            DataTable table = theCompany.GetListForCompanyLevel().Tables[0];
            foreach (DataRow dr in table.Rows)
            {
                BindTreeNode(dr["id"].ToString(), dr["LevelName"].ToString(), dr["id"].ToString(), dr["LevelName"].ToString(), 1,2);
            }

        }

        private void ResolveTreeNode(ExtAspNet.TreeNodeCollection nodes)
        {
            foreach (ExtAspNet.TreeNode node in nodes)
            {
                if (node.Nodes.Count == 0)
                {
                    if (!String.IsNullOrEmpty(node.NavigateUrl))
                    {
                        node.Target = "main";
                    }
                }
                else
                {
                    ResolveTreeNode(node.Nodes);
                }
            }

        }

        void BindProvenctList()
        {
            string sWhere = "";
            if (!string.IsNullOrEmpty(base.CompanyId))
            {
                sWhere = string.Format(" and ( id ={0} or parentId={0})", base.CompanyId);
            }
            DataSet ds = theCompany.GetProvenceList(sWhere);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                BindTreeNode(dr["provence"].ToString(), dr["provence"].ToString(), dr["provence"].ToString(), dr["provence"].ToString(), 1,1);
            }
  
        }
        void BindCompanyTree()
        {
            DataTable dt;
            string sWhere = " 1=1 ";
            if (!string.IsNullOrEmpty(base.CompanyId))
            {
                sWhere = string.Format(" id={0} or parentId={0} ",base.CompanyId);
            }

            dt = theCompany.GetListForTree(sWhere).Tables[0];
            this.treeMenu.Items.Clear();
            if (!string.IsNullOrEmpty(base.CompanyId))
            {
                dt.DefaultView.RowFilter = string.Format("levelId = {0}", base.CompanyLevelId);
            }
            else
            {
                dt.DefaultView.RowFilter = "levelId = 1";
            }
            DataView dtLevelI =  dt.DefaultView ;
            dtLevelI.Sort = "CompanyShortName ASC";

            for(int i=0;i<dtLevelI.Count;i++)
            {
                BindTreeNode(dtLevelI[i]["id"].ToString(), dtLevelI[i]["CompanyShortName"].ToString(), dtLevelI[i]["id"].ToString(), dtLevelI[i]["CompanyShortName"].ToString(), 1,0);
            }
            dt.DefaultView.RowFilter = "levelId = 2 or levelId = 3 or levelId = 4 ";
            DataView dtLevelII = dt.DefaultView;
            dtLevelII.Sort = "CompanyShortName ASC";
            for (int i = 0; i < dtLevelII.Count; i++)
            {
                BindTreeNode(dtLevelII[i]["ParentId"].ToString(), dtLevelII[i]["ParentName"].ToString(), dtLevelII[i]["id"].ToString(), dtLevelII[i]["CompanyShortName"].ToString(),  2,0);
            }
            dt.DefaultView.Sort = "CompanyShortName ASC";

            for (int i = 0; i < dt.DefaultView.Count; i++)
            {
                DataRowView dr = dt.DefaultView[i];
                BindTreeNode(dr["provence"].ToString(), dr["provence"].ToString(), dr["id"].ToString(), dr["CompanyShortName"].ToString(), 2, 1);
                BindTreeNode(dr["levelId"].ToString(), dr["CompanyLevel"].ToString(), dr["id"].ToString(), dr["CompanyShortName"].ToString(), 2, 2);
            }
            
        }

        void BindTreeNode(string parentId, string parentName, string subId, string subName, int level,int treeId)
        {
            ExtAspNet.TreeNode node;
            int nodeIndex = FindNodeById(parentId);
            if (nodeIndex < 0)
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = parentId;
                node.SingleClickExpand = true;
                node.Text = parentName;
                node.Target = "main";  
                if (level == 1)
                {
                    nodeIndex = FindNodeById(parentId);
                    if (treeId == 0)
                    {
                        node.NavigateUrl = "openCardList.aspx?id=" + node.NodeID;
                        this.treeMenu.Nodes.Add(node);
                    }
                    else if (treeId == 1)
                    {
                        this.treeMenuProvence.Nodes.Add(node);
                    }
                    else if (treeId == 2)
                    {
                        this.treeMenuLevel.Nodes.Add(node);
                    }
                }    
            }

            node = new ExtAspNet.TreeNode();
            node.NodeID = subId;
            node.SingleClickExpand = true;
            node.Target = "main";
            node.NavigateUrl = "openCardList.aspx?id=" + node.NodeID;
            node.Text = subName;
            if (treeId == 0)
            {
                if (this.treeMenu.FindNode(parentId) != null && level != 1)
                {
                    this.treeMenu.FindNode(parentId).Nodes.Add(node);
                }
            }
            else if (treeId == 1)
            {
                if (this.treeMenuProvence.FindNode(parentId) != null && parentId != subId)
                {
                    this.treeMenuProvence.FindNode(parentId).Nodes.Add(node);
                }
            }
            else if (treeId == 2)
            {
                if (this.treeMenuLevel.FindNode(parentId) != null && parentId != subId)
                {
                    this.treeMenuLevel.FindNode(parentId).Nodes.Add(node);
                }
            }

        }

        int FindNodeById(string sNodeId)
        {
            for (int i = 0; i < this.treeMenu.Nodes.Count; i++)
            {
                if (this.treeMenu.Nodes[i].NodeID == sNodeId)
                {
                    return i;
                }
            }
            return -1;

        }

        protected void ddlCompanyLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindCompanyTree();
        }


        void BindCompanyTreeProvence()
        {
            DataTable dt;
            string sWhere = " 1=1 ";
            dt = theCompany.GetListForTree(sWhere).Tables[0];
            foreach(DataRow dr in dt.Rows)
            {
                BindTreeNode(dr["provence"].ToString(), dr["provence"].ToString(), dr["id"].ToString(), dr["CompanyShortName"].ToString(), 2,1);
            }

        }


 
    }
}